<!DOCTYPE html>
<html lang="en">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <title>智能组卷系统</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="app-container">
        <div class="box">
            <!-- 筛选器 -->
            <div class="filter-container" style="padding:10px 0 15px 800px;">
                <el-input placeholder="名称" v-model="pagination.selection" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="handleSelect()" class="dalfBut">查询</el-button>
                <el-button @click="resetSelection()" class="dalfBut">重置</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新增模板</el-button>
            </div>

            <!-- 数据表格 -->
            <el-table size="small" current-row-key="id" min-row-height="40px" :data="dataList" border>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column label="模板名" align="center" min-width="250px">
                    <template slot-scope="scope">
                        <div style="white-space: nowrap; text-overflow: ellipsis; overflow: hidden;">
                            {{scope.row.name }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="score" label="总分" align="center"></el-table-column>

                <el-table-column label="选择题" align="center" min-width="120px">
                    <el-table-column prop="choiceNumber" label="题数" align="center" min-width="60px"></el-table-column>
                    <el-table-column prop="choiceScore" label="总分" align="center" min-width="60px"></el-table-column>
                </el-table-column>
                <el-table-column label="填空题" align="center" min-width="120px">
                    <el-table-column prop="fillNumber" label="题数" align="center" min-width="60px"></el-table-column>
                    <el-table-column prop="fillScore" label="总分" align="center" min-width="60px"></el-table-column>
                </el-table-column>
                <el-table-column label="判断题" align="center" min-width="120px">
                    <el-table-column prop="judgmentNumber" label="题数" align="center" min-width="60px"></el-table-column>
                    <el-table-column prop="judgmentScore" label="总分" align="center" min-width="60px"></el-table-column>
                </el-table-column>
                <el-table-column label="简答题" align="center" min-width="120px">
                    <el-table-column prop="shortAnswerNumber" label="题数" align="center"
                                     min-width="60px"></el-table-column>
                    <el-table-column prop="shortAnswerScore" label="总分" align="center"
                                     min-width="60px"></el-table-column>
                </el-table-column>
                <el-table-column label="计算题" align="center" min-width="120px">
                    <el-table-column prop="calculationNumber" label="题数" align="center"
                                     min-width="60px"></el-table-column>
                    <el-table-column prop="calculationScore" label="总分" align="center"
                                     min-width="60px"></el-table-column>
                </el-table-column>
                <el-table-column label="其它题" align="center" min-width="120px">
                    <el-table-column prop="otherNumber" label="题数" align="center" min-width="60px"></el-table-column>
                    <el-table-column prop="otherScore" label="总分" align="center" min-width="60px"></el-table-column>
                </el-table-column>

                <el-table-column label="操作" align="center" min-width="200px">
                    <template slot-scope="scope">
                        <el-button v-if="userInfo.status===2" plain type="text" size="mini"
                                   @click="handleUpdate(scope.row)">
                            查看/编辑
                        </el-button>
                        <el-button v-if="userInfo.status===1" plain type="text" size="mini"
                                   @click="handleUpdate(scope.row)">
                            查看
                        </el-button>
                        <el-button plain type="text" size="mini" @click="handleDetails(scope.row)">
                            详情
                        </el-button>
                        <el-button v-if="userInfo.status===2" plain type="text" size="mini"
                                   @click="handleDelete(scope.row)" style="color: red;">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <div class="pagination-container">
                <el-pagination
                        class="pagination"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total,prev,pager,next,jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增弹窗 -->
            <div class="add-form">
                <el-dialog title="添加模板" :visible.sync="addFormVisible">
                    <el-form ref="dataAddForm" :model="formData" label-position="right">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="模板名称" prop="name" label-width="100px">
                                    <el-input v-model="formData.name" placeholder="未命名"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="试卷总分" prop="score" label-width="100px">
                                    <el-input v-model="formData.score" placeholder="100"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="7">
                                <el-form-item label="选择题:题数" prop="choiceNumber" label-width="115px">
                                    <el-input v-model="formData.choiceNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="choiceScore" label-width="60px">
                                    <el-input v-model="formData.choiceScore"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="7">
                                <el-form-item label="填空题:题数" prop="fillNumber" label-width="115px">
                                    <el-input v-model="formData.fillNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="fillScore" label-width="60px">
                                    <el-input v-model="formData.fillScore"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="7">
                                <el-form-item label="判断题:题数" prop="judgmentNumber" label-width="115px">
                                    <el-input v-model="formData.judgmentNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="judgmentScore" label-width="60px">
                                    <el-input v-model="formData.judgmentScore"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="7">
                                <el-form-item label="简答题:题数" prop="shortAnswerNumber" label-width="115px">
                                    <el-input v-model="formData.shortAnswerNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="shortAnswerScore" label-width="60px">
                                    <el-input v-model="formData.shortAnswerScore"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="7">
                                <el-form-item label="计算题:题数" prop="calculationNumber" label-width="115px">
                                    <el-input v-model="formData.calculationNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="calculationScore" label-width="60px">
                                    <el-input v-model="formData.calculationScore"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="7">
                                <el-form-item label="其它题:题数" prop="otherNumber" label-width="115px">
                                    <el-input v-model="formData.otherNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="otherScore" label-width="60px">
                                    <el-input v-model="formData.otherScore"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>

                    <div slot="footer" class="dialog-footer">
                        <el-button @click="addFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑弹窗 -->
            <div class="add-form">
                <el-dialog title="模板数据" :visible.sync="editFormVisible">
                    <el-form ref="dataAddForm" :model="formData" label-position="right" :disabled="isFormDisabled()">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="模板名称" prop="name" label-width="100px">
                                    <el-input v-model="formData.name" placeholder="未命名"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="试卷总分" prop="score" label-width="100px">
                                    <el-input v-model="formData.score" placeholder="100"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="7">
                                <el-form-item label="选择题:题数" prop="choiceNumber" label-width="115px">
                                    <el-input v-model="formData.choiceNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="choiceScore" label-width="60px">
                                    <el-input v-model="formData.choiceScore"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="7">
                                <el-form-item label="填空题:题数" prop="fillNumber" label-width="115px">
                                    <el-input v-model="formData.fillNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="fillScore" label-width="60px">
                                    <el-input v-model="formData.fillScore"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="7">
                                <el-form-item label="判断题:题数" prop="judgmentNumber" label-width="115px">
                                    <el-input v-model="formData.judgmentNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="judgmentScore" label-width="60px">
                                    <el-input v-model="formData.judgmentScore"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="7">
                                <el-form-item label="简答题:题数" prop="shortAnswerNumber" label-width="115px">
                                    <el-input v-model="formData.shortAnswerNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="shortAnswerScore" label-width="60px">
                                    <el-input v-model="formData.shortAnswerScore"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="7">
                                <el-form-item label="计算题:题数" prop="calculationNumber" label-width="115px">
                                    <el-input v-model="formData.calculationNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="calculationScore" label-width="60px">
                                    <el-input v-model="formData.calculationScore"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="7">
                                <el-form-item label="其它题:题数" prop="otherNumber" label-width="115px">
                                    <el-input v-model="formData.otherNumber"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-form-item label=" 总分" prop="otherScore" label-width="60px">
                                    <el-input v-model="formData.otherScore"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>

                    <div slot="footer" class="dialog-footer" v-if="userInfo.status===2">
                        <el-button @click="editFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 详情弹窗 -->
            <div class="add-form">
                <el-dialog title="题型分值占比" :visible.sync="detailFormVisible"
                           :close-on-press-escape="false"
                           :close-on-click-modal="false">
                    <div ref="chart" style="width: 100%; height: 400px;"></div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios.min.js"></script>
<script src="/webjars/echarts/4.9.0/echarts.min.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {
                currentPage: 1,  //当前页码
                pageSize: 9,    //每页记录
                total: 0,        //总记录
                selection: "",    //搜索框
            },
            dataList: [],//当前页要展示的列表数据
            formData: {},//表单数据
            addFormVisible: false,//控制表单是否可见
            editFormVisible: false,//编辑表单是否可见
            detailFormVisible: false,

            chartData: null
        },

        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            const userInfo = window.localStorage.getItem('userInfo')
            if (userInfo) {
                this.userInfo = JSON.parse(userInfo)
            }
            this.getAll();
        },
        methods: {
            getAll() {
                param = "?name=" + this.pagination.selection;
                console.log(param);
                axios.get("/model/" + this.pagination.currentPage + "/" + this.pagination.pageSize + param).then((res) => {
                    this.pagination.pageSize = res.data.data.size;
                    this.pagination.currentPage = res.data.data.current;
                    this.pagination.total = res.data.data.total;
                    this.dataList = res.data.data.records;
                });
            },

            //搜索
            handleSelect() {
                this.getAll();
            },

            //重置搜索框
            resetSelection() {
                this.pagination.selection = "";
                this.getAll();
            },

            //分页
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.getAll()
            },

            //弹出添加窗口
            handleCreate() {
                this.addFormVisible = true;
                this.resetForm();
            },

            //重置表单
            resetForm() {
                this.formData = {};
            },

            //添加模板
            handleAdd() {
                //发送ajax请求
                axios.post("/model", this.formData).then((res) => {
                    //如果操作成功，关闭弹层，显示数据
                    if (res.data.code === 20010) {
                        this.addFormVisible = false;
                        this.$message.success("添加成功");
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(() => {
                    this.getAll();
                });
            },

            //弹出编辑窗口
            handleUpdate(row) {
                axios.get("/model/" + row.id).then((res) => {
                    if (res.data.code === 20040) {
                        //展示弹层，加载数据
                        this.formData = res.data.data;
                        this.editFormVisible = true;
                    } else {
                        this.$message.error(res.data.msg);
                    }
                });
            },

            //编辑
            handleEdit() {
                //发送ajax请求
                axios.put("/model", this.formData).then((res) => {
                    //如果操作成功，关闭弹层，显示数据
                    if (res.data.code === 20030) {
                        this.editFormVisible = false;
                        this.$message.success("修改成功");
                    } else if (res.data.code === 20031) {
                        this.$message.error(res.data.msg);
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(() => {
                    this.getAll();
                });
            },

            //表单是否可编辑
            isFormDisabled() {
                if (this.userInfo.status === 1)
                    return true; // 这里返回true表示表单被禁用
                return false
            },

            //逻辑删除
            handleDelete(row) {
                this.$confirm("此操作永久删除当前数据，是否继续？", "提示", {
                    type: 'info'
                }).then(() => {
                    row.status = 0;
                    axios.put("/model/delete", row).then((res) => {
                        if (res.data.code === 20020) {
                            this.$message.success("删除成功");
                        } else {
                            this.$message.error("删除失败");
                        }
                    }).finally(() => {
                        this.getAll();
                    });
                }).catch(() => {
                    //3.取消删除
                    this.$message.info("取消删除操作");
                });
            },

            handleDetails(row) {
                this.detailFormVisible = true;
                // 处理数据
                const data = [];
                if (row.choiceScore > 0) {
                    data.push({
                        name: '选择题',
                        value: row.choiceScore
                    });
                }
                if (row.fillScore > 0) {
                    data.push({
                        name: '填空题',
                        value: row.fillScore
                    });
                }
                if (row.judgmentScore > 0) {
                    data.push({
                        name: '判断题',
                        value: row.judgmentScore
                    });
                }
                if (row.shortAnswerScore > 0) {
                    data.push({
                        name: '简答题',
                        value: row.shortAnswerScore
                    });
                }
                if (row.calculationScore > 0) {
                    data.push({
                        name: '计算题',
                        value: row.calculationScore
                    });
                }
                if (row.otherScore > 0) {
                    data.push({
                        name: '其它题',
                        value: row.otherScore
                    });
                }
                // 绘制饼状图

                this.$nextTick(() => {
                    console.log(this.$refs.chart)
                    this.chartData = echarts.init(this.$refs.chart);
                    this.chartData.setOption({
                        series: [{
                            name: '模板题型占比',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '50%'],
                            label: {
                                formatter: '{b}: {d}%'
                            },
                            data: data
                        }]
                    });
                });

            }

        }
    })
</script>
</html>